package com.android.clinic.app.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.layout.LinearLayoutHelper;

/**
 * Created by cheng on 2018/1/26.
 */
public abstract class BaseDelegateAdapter extends DelegateAdapter.Adapter<RecyclerView.ViewHolder> {

    protected Context mContext;
    private LayoutInflater mInflater;
    private int resources[];

    public BaseDelegateAdapter(Context context, int... resource) {
        mContext = context;
        this.resources = resource;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return new LinearLayoutHelper();
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public void onViewRecycled(RecyclerView.ViewHolder holder) {
        super.onViewRecycled(holder);
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        int typeIndex = getItemViewType(viewType);
        View convertView = mInflater.inflate(resources[typeIndex], parent, false);
        return (BaseViewHolder) setupViewHolder(convertView, parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        int typeIndex = getItemViewType(position);
        setupBindViewHolder(holder, position, typeIndex);
    }

    protected abstract Object setupViewHolder(@Nullable View convertView, ViewGroup parent, int typeIndex);

    protected abstract void setupBindViewHolder(Object viewHolder, int position, int typeIndex);

    protected static class BaseViewHolder extends RecyclerView.ViewHolder {

        public BaseViewHolder(View itemView) {
            super(itemView);
        }
    }
}
